package com.sg.hms.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.sampled.AudioFormat.Encoding;
import javax.swing.filechooser.FileSystemView;

import com.sg.hms.Dao.DataGridDaochuDao;
import com.sg.hms.DaoImpl.DataGridDaochuDaoImpl;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * 廖彦
 * @author q
 *
 */
public class DataGridDaochuController extends HttpServlet{
	    DataGridDaochuDao d = new DataGridDaochuDaoImpl();
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        	// TODO Auto-generated method stub
        	doPost(req, resp);
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        	StringBuffer s = d.chazhaoApplication();
        	dachu(s.toString(), resp);
            req.getRequestDispatcher("xianshiEmployees");
        }

    	// 分割字符串，写入excel的方法
    	public void dachu(String data,HttpServletResponse response) {
    		response.setContentType("application/octet-stream"); //重点
    		response.setCharacterEncoding("utf-8");//重点
    		String name = "Test.xls";
    		try {
				name = URLEncoder.encode(name, "utf-8");//重点
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
    		response.addHeader("Content-Disposition", "attachment;filename=" + name);//重点
    		
    		
    		// 第一次分割，把字符串分割成多少行
    		String[] arr1 = data.split("#");
    		// 定义一个二维数组,行数为第一次分割的数组的长度+1(表头长度为1)，列数为7
    		String[][] arr3 = new String[arr1.length+1][7];
    		for (int i = 0; i < arr1.length; i++) {
    			// 第二次分割,循环分割每一行
    			String arr2[] = arr1[i].split(" ");
    			for (int j = 0; j < 7; j++) {
    				arr3[i][j] = arr2[j];
    			}
    		}
    		// 创建Excel工作薄 
    		WritableWorkbook wwb;
    		try {
    			//创建一个输出流
    			OutputStream os = response.getOutputStream();
    			wwb=Workbook.createWorkbook(os);
    			//创建一个工作页，第一个参数的页名，第二个参数表示该工作页在excel中处于哪一页
    			WritableSheet sheet = wwb.createSheet("申请记录", 0);
    			//设置所有列的列宽
    			for (int i = 1; i<=7; i++) {
    				sheet.setColumnView(i, 15);
    			}
    			//写入表头
    			Label label1 = new Label(0,0,"申请编号");//填充第一行第一个单元格的内容
    			sheet.addCell(label1);
    			Label label2 = new Label(1,0,"创建日期");//填充第一行第一个单元格的内容
    			sheet.addCell(label2);
    			Label label3 = new Label(2,0,"申请人");//填充第一行第一个单元格的内容
    			sheet.addCell(label3);
    			Label label4 = new Label(3,0,"申请所属部门");//填充第一行第一个单元格的内容
    			sheet.addCell(label4);
    			Label label5 = new Label(4,0,"申请金额");//填充第一行第一个单元格的内容
    			sheet.addCell(label5);
    			Label label6 = new Label(5,0,"审批状态");//填充第一行第一个单元格的内容
    			sheet.addCell(label6);
    			Label label7 = new Label(6,0,"申请内容");//填充第一行第一个单元格的内容
    			sheet.addCell(label7);
    			//循环写入表内容
    			for (int i = 0; i < arr1.length; i++) {
    				for (int j = 0; j < 7; j++) {
    					Label label = new Label(j,i+1,arr3[i][j]);//填充第一行第一个单元格的内容
    					sheet.addCell(label);
    				}
    			}
    			wwb.write();//将内容写到excel文件中
    			os.flush();//清空输出流
    			wwb.close();
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (RowsExceededException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (WriteException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} 
    		
    	}
}
